var table = null;
layui.use(['table', 'form', 'laydate', 'element'], function () {
    table = layui.table;
    var form = layui.form;
    var laydate = layui.laydate;
    var element = layui.element;

    /*初始化表单元素*/
    laydate.render({
        elem: '#createTime'
        , type: 'datetime'
    });

    /*初始化数据表格*/
    table.render({
        elem: '#dataGrid'
        , url: '/admin/blog/list'
        , method: 'post'
        , page: true
        , even: true //开启隔行背景
        // ,size: 'sm'
        , cols: [[
            {type: 'numbers', fixed: "left"}
            , {type: 'checkbox', fixed: "left"}
            , {field: 'title', title: '标题', width: 100}
            , {field: 'createTimeStr', title: '时间', width: 180}
            , {fixed: 'right', width: 300, toolbar: '#bars'}
        ]]
    });

    //监听查询
    form.on('submit(search)', function (data) {
        table.reload('dataGrid', {
            where: data.field
        });
        return false;
    });

    //监听提交
    form.on('submit(save)', function (data) {
        layer.confirm('确定提交吗?', {icon: 3, title: '提示', offset: '150px'}, function (index) {
            if (ue.getContent() != "") {
                data.field['summary'] = ue.getContent().substr(0, 800);
            }
            if (ue.getContentTxt() != "") {
                data.field['contentNoTag'] = ue.getContentTxt();
            }
            $.ajax({
                type: "post",
                url: "/admin/blog/save",
                data: data.field,
                dataType: "json",
                success: function (result) {
                    if (result.success == true) {
                        layer.close(index);
                        layer.msg(result.msg, {
                            icon: 6,
                            offset: '150px',
                            time: 1000
                        }, function () {
                            window.location.href = '/admin/blog/index'
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 5,
                            offset: '150px'
                        });
                    }
                }
            });
        });
        return false;
    });

    //监听工具条
    table.on('tool(dataGrid)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        // var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        // var tr = obj.tr; //获得当前行 tr 的DOM对象

        if (layEvent === 'detail') { //查看
            window.location.href = '/admin/blog/add?id=' + obj.data.id + "&type=look"
        } else if (layEvent === 'del') {
            //删除
        } else if (layEvent === 'edit') { //编辑

        }
    });

    /*绑定工具栏*/
    var $ = layui.$, active = {
        barTableAdd: function () { //获取选中数据
            window.location.href = '/admin/blog/add'
            // layer.open({
            //     type: 1,
            //     title: '新增|编辑框',
            //     content: $('#editForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            // });
            // layer.open({
            //     type: 2,
            //     title: '新增|编辑框',
            //     shadeClose: true,
            //     shade: false,
            //     maxmin: true, //开启最大化最小化按钮
            //     area: [$(window).width()+"px",$(window).height()+"px"],
            //     content: '/admin/blog/add'
            // });
        }
        , barTableEdit: function () { //获取选中数目
            var checkStatus = table.checkStatus('dataGrid');
            if (checkStatus.data.length == 0) {
                layer.msg("未选中数据", {
                    icon: 5,
                    offset: '150px'
                });
            } else if (checkStatus.data.length > 1) {
                layer.msg("只能勾选一条数据", {
                    icon: 5,
                    offset: '150px'
                });
            } else {
                window.location.href = '/admin/blog/add?id=' + checkStatus.data[0].id
            }
        }
        , barTableRemove: function () {
            var checkStatus = table.checkStatus('dataGrid');
            if (checkStatus.data.length == 0) {
                layer.msg("未选中数据", {
                    icon: 5,
                    offset: '150px'
                });
            } else {
                layer.confirm('确定删除吗?', {icon: 3, title: '提示', offset: '150px'}, function (index) {
                    var ids = new Array();
                    for (index in checkStatus.data) {
                        var id = checkStatus.data[index].id;
                        ids.push(id);
                    }
                    console.log(ids)
                    $.ajax({
                        type: "post",
                        url: "/admin/blog/delete",
                        data: {'ids': ids},
                        dataType: "json",
                        success: function (result) {
                            if (result.success == true) {
                                layer.close(index);
                                layer.msg(result.msg, {
                                    icon: 6,
                                    offset: '150px',
                                    time: 1000
                                }, function () {
                                    table.reload('dataGrid', {
                                        where: $('#searchForm').serializeJSON()
                                    });
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 5,
                                    offset: '150px'
                                });
                            }
                        }
                    });
                });
            }
        }
    };

    $('.barTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});






